function sb = fsb_get_hemodynamics_er(d,sandbox)

% FSB : Event related design selection GUI
%
% EXAMPLE: sb = fsb_get_hemodynamics_er(150,2)
%
% INPUT:
% d: number of volumes
% sandbox: sandbox experiment struct;
%
% OUTPUT:
% sb: sandbox struct for event related design
%
% NOTES:
% Use this GUI to set up your experiments. You can enter information on up
% to 4 Event types
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
%
% $ Revision 1.0
%
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Set GUI up
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~

selwindow = figure('Visible','off','Position',[100,100,590,280],'Resize','off','Toolbar','none','MenuBar','none','NumberTitle','off','Name','Event related design setup');
figure(selwindow);

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Define blockdata struct
%~~~~~~~~~~~~~~~~~~~~~~~~~~
erdata(1).eventon = [];
erdata(1).eventdur = [];
erdata(1).eventname = 'Event1';
event_load = 0;
eventshow = [0 0 0 0];
dispderiv= 0;
trialon = [];
trialdur = [];
dummytrig = [];
sb = [];

try
    f_name = sandbox.name;
catch
    f_name = 'Placeholder';
end

try
    if sandbox.scanpar.TR > 10
    TR = sandbox.scanpar.TR/1000;
    end
catch
    TR = sandbox.TR;
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Add Buttons and edit fields
%~~~~~~~~~~~~~~~~~~~~~~~~~~

uicontrol('Style','Text',...
    'Position',[10,260,50,15],...
    'String','TR:');


h_TR = uicontrol('Style','Edit',...
    'Position',[50,260,50,15],...
    'String',num2str(TR),...
    'Callback',{@TR_Callback});

uicontrol('Style','Text',...
    'Position',[100,260,50,15],...
    'String','Dataset Name');

h_name = uicontrol('Style','Edit',...
    'Position',[200,260,200,15],...
    'String',f_name,...
    'Callback',{@name_Callback});



%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Trial definitions
%~~~~~~~~~~~~~~~~~~~~~~~~~~

h_dispderiv = uicontrol('Style','Radiobutton',...
    'Position',[450,250,100,25],...
    'String','Derivatives',...
    'TooltipString','Time and dispersion derivatives',...
    'Callback',{@dispderiv_Callback});

uicontrol ('Style','Pushbutton',...
    'Position',[450,160,110,25],...
    'String','Load from File',...
    'TooltipString','Load experiments/matrices',...
    'Callback',{@loadfile_Callback});

uicontrol('Style','Text',...
    'Position',[450,130,100,15],...
    'String','Dummy Triggers');

h_dummy = uicontrol('Style','Edit',...
    'Position',[550,130,20,15],...
    'String','0',...
    'Callback',{@dummy_Callback});

uicontrol('Style','Pushbutton',...
    'Position',[450,220,110,25],...
    'String','Done',...
    'TooltipString','Done with setup',...
    'Callback',{@eventdone_Callback});

uicontrol('Style','Pushbutton',...
    'Position',[450,190,110,25],...
    'String','Preview',...
    'TooltipString','Show model',...
    'Callback',{@showmodel_Callback});

%~~~~~~~~~~~~~~~~~~~~~~~~
% Event definitions
%~~~~~~~~~~~~~~~~~~~~~~~~

uicontrol ('Style','Text',...
    'Position',[60,240,130,15],...
    'String','Duration')

uicontrol ('Style','Text',...
    'Position',[200,240,200,15],...
    'String','Onset vector')

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Trials
%~~~~~~~~~~~~~~~~~~~~~~~~~~

uicontrol ('Style','Text',...
    'Position',[10,225,50,15],...
    'String','Trials',...
    'TooltipString','Trial duration vector')

h_trialdur = uicontrol('Style','edit',...
    'Position',[60,225,130,15],...
    'String',num2str(trialdur),...
    'TooltipString','Trial duration vector',...
    'Callback',{@trialdur_Callback});

h_trialonset = uicontrol('Style','edit',...
    'Position',[200,225,200,15],...
    'String',num2str(trialon),...
    'TooltipString','Trial onset vector',...
    'Callback',{@trialonset_Callback});

uicontrol ('Style','Text',...
    'Position',[400,240,35,15],...
    'String','Show',...
    'TooltipString','Trial duration vector')

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Event 1
%~~~~~~~~~~~~~~~~~~~~~~~~~~

h_eventname1 = uicontrol ('Style','Edit',...
    'Position',[10,200,50,15],...
    'String','Event 1',...
    'Callback',{@eventname1_Callback});

h_eventdur1 = uicontrol('Style','edit',...
    'Position',[60,200,130,15],...
    'String',num2str(erdata(1).eventdur),...
    'TooltipString','Event duration (s)',...
    'Callback',{@eventdur1_Callback});

h_eventon1 = uicontrol('Style','edit',...
    'Position',[200,200,200,15],...
    'String',num2str(erdata(1).eventon),...
    'TooltipString','Event onsets',...
    'Callback',{@eventon1_Callback});

h_eventshow1 = uicontrol('Style','Radiobutton',...
    'Position',[410,200,15,15],...
    'TooltipString','Show',...
    'Callback',{@eventshow1_Callback});

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Event 2
%~~~~~~~~~~~~~~~~~~~~~~~~~~

h_eventname2 = uicontrol ('Style','Edit',...
    'Position',[10,175,50,15],...
    'String','Event 2',...
    'Callback',{@eventname2_Callback});

h_eventdur2 = uicontrol('Style','edit',...
    'Position',[60,175,130,15],...
    'TooltipString','Event duration vector',...
    'Callback',{@eventon_Callback});

h_eventon2 = uicontrol('Style','edit',...
    'Position',[200,175,200,15],...
    'TooltipString','Event onset vector',...
    'Callback',{@eventon2_Callback});

h_eventshow2 = uicontrol('Style','Radiobutton',...
    'Position',[410,175,15,15],...
    'TooltipString','Show',...
    'Callback',{@eventshow2_Callback});

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Event 3
%~~~~~~~~~~~~~~~~~~~~~~~~~~

h_eventname3 =uicontrol ('Style','Edit',...
    'Position',[10,150,50,15],...
    'String','Event 3',...
    'Callback',{@eventname3_Callback});

h_eventdur3 = uicontrol('Style','edit',...
    'Position',[60,150,130,15],...
    'TooltipString','Event duration (s)',...
    'Callback',{@eventdur3_Callback});

h_eventon3 = uicontrol('Style','edit',...
    'Position',[200,150,200,15],...
    'TooltipString','Event onset vector',...
    'Callback',{@eventon3_Callback});

h_eventshow3 = uicontrol('Style','Radiobutton',...
    'Position',[410,150,15,15],...
    'TooltipString','Show',...
    'Callback',{@eventshow3_Callback});

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Event 4
%~~~~~~~~~~~~~~~~~~~~~~~~~~

h_eventname4 =uicontrol ('Style','Edit',...
    'Position',[10,125,50,15],...
    'String','Event 4',...
    'Callback',{@eventname4_Callback});

h_eventdur4 = uicontrol('Style','edit',...
    'Position',[60,125,130,15],...
    'TooltipString','Event duration (s)',...
    'Callback',{@eventdur4_Callback});

h_eventon4 = uicontrol('Style','edit',...
    'Position',[200,125,200,15],...
    'TooltipString','Event onset vector',...
    'Callback',{@eventon4_Callback});

h_eventshow4 = uicontrol('Style','Radiobutton',...
    'Position',[410,125,15,15],...
    'TooltipString','Show',...
    'Callback',{@eventshow4_Callback});

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Add display axis
%~~~~~~~~~~~~~~~~~~~~~~~~~~

graphax = axes('Units','Pixels','Position',[20,20,550,100]);

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Wait until user input is done
%~~~~~~~~~~~~~~~~~~~~~~~~~~

uiwait;

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Button callbacks
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% TR callback
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function TR_Callback(source, eventdata)
        TR = get(h_TR,'String');
        set(h_TR,'String',num2str(TR));
        sandbox.TR = str2num(TR);
        TR = sandbox.TR;
    end

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Dummy callback
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function dummy_Callback(source, eventdata)
        dummytrig = get(h_dummy,'String');
        set(h_dummy,'String',num2str(dummytrig));
        sandbox.dummytrig = str2num(dummytrig);
        dummytrig = sandbox.dummytrigs;
    end

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Name callback
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function name_Callback(source, eventdata)
        f_name = get(h_name,'String');
        set(h_name,'String',sandbox.name);
        sandbox.name = f_name;
    end

% ~~~~~~~~~~~~~~~~~~
% Trial callbacks
% ~~~~~~~~~~~~~~~~~~
    function trialonset_Callback(source, eventdata)
        trialon = get(h_trialonset,'String');
        set(h_trialonset,'String',num2str(trialon));
        trialon = str2num(trialon);
    end

    function trialdur_Callback(source, eventdata)
        trialdur = get(h_trialdur,'String');
        set(h_trialdur,'String',num2str(trialdur));
        trialdur = str2num(trialdur);
    end

% ~~~~~~~~~~~~~~~~~
% Display derivatives callback
% ~~~~~~~~~~~~~~~~~

    function dispderiv_Callback(source, eventdata)

        if (get(h_dispderiv,'Value') == get(h_dispderiv,'Max'))
            dispderiv = 1;
        else
            dispderiv = 0;
        end
        showmodel_Callback;
    end

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Event callbacks
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~
% Event one
%~~~~~~~~~~~
    function eventname1_Callback(source, eventdata)
        eventname1 = get(h_eventname1,'String');
        set(h_eventname1,'String',num2str(eventname1));
        erdata(1).eventname = str2num(eventname1);
        showmodel_Callback;
    end

    function eventon1_Callback(source, eventdata)
        eventon1 = get(h_eventon1,'String');
        set(h_eventon1,'String',num2str(eventon1));
        erdata(1).eventon = str2num(eventon1);
    end

    function eventdur1_Callback(source, eventdata)
        eventdur1 = get(h_eventdur1,'String');
        set(h_eventdur1,'String',num2str(eventdur1));
        erdata(1).eventdur = str2num(eventdur1);
    end

    function eventshow1_Callback(source, eventdata)
        if (get(h_eventshow1,'Value') == get(h_eventshow1,'Max'))
            eventshow(1) = 1;
        else
            eventshow(1) = 0;
        end
        showmodel_Callback;
    end

%~~~~~~~~~~~
% Event two
%~~~~~~~~~~~

    function eventname2_Callback(source, eventdata)
        eventname2 = get(h_eventname2,'String');
        set(h_eventname2,'String',num2str(eventname2));
        erdata(2).eventname = str2num(eventname2);
        showmodel_Callback;
    end

    function eventon2_Callback(source, eventdata)
        eventon2 = get(h_eventon2,'String');
        set(h_eventon2,'String',num2str(eventon2));
        erdata(2).eventon = str2num(eventon2);
    end

    function eventdur2_Callback(source, eventdata)
        eventdur2 = get(h_eventdur2,'String');
        set(h_eventdur2,'String',num2str(eventdur2));
        erdata(2).eventdur = str2num(eventdur2);
    end

    function eventshow2_Callback(source, eventdata)
        if (get(h_eventshow2,'Value') == get(h_eventshow2,'Max'))
            eventshow(2) = 1;
        else
            eventshow(2) = 0;
        end
        showmodel_Callback;
    end

%~~~~~~~~~~~
% Event three
%~~~~~~~~~~~
    function eventname3_Callback(source, eventdata)
        eventname3 = get(h_eventname3,'String');
        set(h_eventname3,'String',num2str(eventname3));
        erdata(3).eventname = str2num(eventname3);
        showmodel_Callback;
    end

    function eventon3_Callback(source, eventdata)
        eventon3 = get(h_eventon3,'String');
        set(h_eventon3,'String',num2str(eventon3));
        erdata(3).eventon = str2num(eventon3);
        showmodel_Callback;
    end

    function eventdur3_Callback(source, eventdata)
        eventdur3 = get(h_eventdur3,'String');
        set(h_eventdur3,'String',num2str(eventdur3));
        erdata(3).eventdur = str2num(eventdur3);
        showmodel_Callback;
    end

    function eventshow3_Callback(source, eventdata)
        if (get(h_eventshow3,'Value') == get(h_eventshow3,'Max'))
            eventshow(3) = 1;
        else
            eventshow(3) = 0;
        end
        showmodel_Callback;
    end

%~~~~~~~~~~~
% Event four
%~~~~~~~~~~~

    function eventname4_Callback(source, eventdata)
        eventname4 = get(h_eventname4,'String');
        set(h_eventname4,'String',num2str(eventname4));
        erdata(4).eventname = str2num(eventname4);
        showmodel_Callback;
    end

    function eventon4_Callback(source, eventdata)
        eventon4 = get(h_eventon4,'String');
        set(h_eventon4,'String',num2str(eventon4));
        erdata(4).eventon = str2num(eventon4);
        showmodel_Callback;
    end

    function eventdur4_Callback(source, eventdata)
        eventdur4 = get(h_eventdur4,'String');
        set(h_eventdur4,'String',num2str(eventdur4));
        erdata(4).eventdur = str2num(eventdur4);
        showmodel_Callback;
    end

    function eventshow4_Callback(source, eventdata)
        if (get(h_eventshow4,'Value') == get(h_eventshow4,'Max'))
            eventshow(4) = 1;
        else
            eventshow(4) = 0;
        end
        showmodel_Callback;
    end
    
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Load existing experiment files
%~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function loadfile_Callback(source, eventdata)

        file_name = uipickfiles( 'Output','char','REF','.dgz|.mat|.rtp');

        [pathstr, name, ext, versn] = fileparts(file_name);

        if ~isempty(ext)
            if strcmp(ext, '.mat') ==1

                %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                % Check first if sandbox struct exists
                %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                try
                    temp = load(file_name,'sandbox');

                    sb = temp.sandbox;
                    sbload =1;

                    for x = 1:size(sb.volmat.evt_dur,2)

                        try
                            erdata(x).eventdur = sb.volmat.evt_dur{x};
                            erdata(x).eventon = sb.volmat.evt_onsets{x};
                        catch
                            erdata(x).eventdur = sb.volmat.evt_dur(x);
                            erdata(x).eventon = sb.volmat.evt_onsets{x};
                        end

                    end
                    disp('Sandbox struct loaded')
                catch
                    sbload = 0;
                end

                try
                    TR = sandbox.TR;
                catch
                end

                if sbload ==0;

                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    % Look for onset vector
                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    try
                        load (file_name,'onset');
                        sb.onset = onset;
                    catch
                        disp('No onset data available')
                    end

                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    % Look for intrial vector
                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    try
                        load (file_name,'intrial');
                        sb.intrial = intrial;
                    catch
                        disp('No intrial data available')
                    end

                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    % Look for hemodynamics vector
                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    try
                        load (file_name,'hemodynamics');
                        sb.hemodynamics = hemodynamics;
                    catch
                        disp('No hemodynamics data available')
                    end

                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    % Look for SPM file
                    % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    try
                        load(file_name,'SPM')
                        sandbox = fsb_import_sandbox(SPM,sandbox);
                    catch
                        disp('No SPM struct found')
                    end

                end

                %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                % Check if .dgz file should be loaded
                %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            elseif strcmp(ext,'.dgz')==1

                event_load = 1;
                sb = fsb_get_mpiexp(file_name,sandbox,d);
                if sb.TR>10
                TR = sb.TR/1000;
                else
                    TR = sb.TR;
                end
                set(h_TR,'String',num2str(TR));
                sandbox.TR = sb.TR;

                for x = 1:size(sb.volmat.evt_dur,2)

                    try
                        erdata(x).eventdur = sb.volmat.evt_dur{x}';
                        erdata(x).eventon = sb.volmat.evt_onsets{x};
                        erdata(x).eventname = sb.volmat.evt_names{x};
                    catch
                        erdata(x).eventdur = sb.volmat.evt_dur(x)';
                        erdata(x).eventon = sb.volmat.evt_onsets{x};
                        erdata(x).eventname = sb.volmat.evt_names{x};
                    end

                end

                %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                % Check if .rtp file should be loaded
                %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            elseif strcmp(ext,'.rtp')==1;
                out = fsb_rtp_read(sandbox,file_name);
                erdata = out.erdata;
                sandbox = out.sandbox;
                sb = out.sandbox;
                trialon = out.trialon;
                trialoff = out.trialoff;
                trialdur = out.trialdur;
                eventshow = [1 0 0 0];
                set(h_eventshow1,'Value',1);
            end

            if ~exist('trialoff','var')
                wholetrial = [0; sb.intrial; 0];
                temp1 = diff(wholetrial);
                temp1 = [temp1; 0];
                trialtemp2 = temp1>0;
                trialtemp = temp1<0;
                trialon = find(trialtemp2==1);
                trialoff = find(trialtemp ==1);
                trialoff = trialoff-1;
                trialdur = (trialoff+1)-trialon;
            end

            % ~~~~~~~~~~~~~~~~~~~~~~
            % Set buttons and checkboxes correctly
            %~~~~~~~~~~~~~~~~~~~~~~

            set(h_trialonset,'String',num2str(trialon'));
            set(h_trialdur,'String',num2str(trialdur'));

            set(h_eventon1,'String',num2str(erdata(1).eventon'));
            set(h_eventon2,'String',num2str(erdata(2).eventon'));
            try
                set(h_eventon3,'String',num2str(erdata(3).eventon'));
                set(h_eventon4,'String',num2str(erdata(4).eventon'));
            catch
            end

            set(h_eventdur1,'String',num2str(erdata(1).eventdur'));
            set(h_eventdur2,'String',num2str(erdata(2).eventdur'));
            try
                set(h_eventdur3,'String',num2str(erdata(3).eventdur'));
                set(h_eventdur4,'String',num2str(erdata(4).eventdur'));
            catch
            end

            set(h_eventname1,'String',num2str(erdata(1).eventname));
            set(h_eventname2,'String',num2str(erdata(2).eventname));
            try
                set(h_eventname3,'String',num2str(erdata(3).eventname));
                set(h_eventname4,'String',num2str(erdata(4).eventname));
            catch
            end
            showmodel_Callback;
        end
    end

%~~~~~~~~~~~~~~~~~
% Finished with event editing
%~~~~~~~~~~~~~~~~~

    function eventdone_Callback(source, eventdata)
        uiresume;
        close(selwindow);
    end

    function showmodel_Callback(source,eventdata)

        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        % define Event onsets and event names
        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if dispderiv == 0;
            evt_names{1} = get(h_eventname1,'String');
            evt_names{2} = get(h_eventname2,'String');
            evt_names{3} = get(h_eventname3,'String');
            evt_names{4} = get(h_eventname4,'String');
            sb.onset = zeros(size(erdata,2),d);

            for x = 1:size(erdata,2)
                evt_dur{x} = erdata(x).eventdur;
                evt_onsets{x} = erdata(x).eventon;
                evt_names{x} = erdata(x).eventname;
                sb.onset(x,round(evt_onsets{x})) = 1;
            end
            plotons = find(eventshow==1);
        else
            evt_names{1} = get(h_eventname1,'String');
            evt_names{2} = get(h_eventname2,'String');
            evt_names{3} = get(h_eventname3,'String');
            evt_names{4} = get(h_eventname4,'String');
            sb.onset = zeros(size(erdata,2),d);

            for x = 1:size(erdata,2)
                evt_dur{x} = erdata(x).eventdur;
                evt_onsets{x} = erdata(x).eventon;
                evt_names{x} = erdata(x).eventname;
                sb.onset(x,round(evt_onsets{x})) = 1;
            end
            plotons = find(eventshow==1);
            otto = 1:3:12;
            plotons=otto(plotons);
        end

        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        % Transfer to onsets
        %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        inparg.evt_onsets = evt_onsets;
        inparg.evt_names = evt_names;
        inparg.evt_dur = evt_dur;
        inparg.TR = TR;
        inparg.volumes = d;
        inparg.deriv =dispderiv;

        try
            [U,bf,p,sb.hemodynamics] = fsb_conv_onsets(inparg);
            sb.onset = sb.onset';

            %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            % Setup intrial array
            %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            sb.discard = zeros(d,1);

            if ~isfield(sb,'intrial') || isempty(sb.intrial) || size(sb.intrial,2)<3

                sb.intrial = zeros(d,3);

                for x = 1:length(trialon)
                    temp = (1:trialdur(x))';
                    sb.intrial(trialon(x):trialon(x)+trialdur(x)-1,1) = 1;
                    sb.intrial(trialon(x):trialon(x)+trialdur(x)-1,2) = temp ;
                    sb.intrial(trialon(x):trialon(x)+trialdur(x)-1,3) = x;
                end

            end

            %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            % Keep everything compatible with FSB
            %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            sb.TR = TR;
            sb.volmat.evt_dur = evt_dur;
            sb.volmat.evt_onsets = evt_onsets;
            sb.volmat.evt_names = evt_names;
            sb.timemat = [];

            sb.discard = [];
            try
                sb.origtrial = max(sb.intrial(:,3));
                sb.hemodynamics = [sb.hemodynamics sb.sensdata];
            catch
                disp('Sensdata file does have a different size from image data')
            end

            sb.discard = [];
            sb.hdr = sandbox.hdr;
            sb.name = f_name;

        catch
            figure(selwindow);
            set(gcf,'CurrentAxes',graphax);cla;
            text (0.1,0.5,'Design size does not match scan size, aborting');

            errmsg = lasterror;
            fsb_error_handler(errmsg);
            disp('Design size does not match scan size, aborting')
            return
        end

        figure(selwindow);
        set(gcf,'CurrentAxes',graphax);cla;
        bar(sb.intrial(:,1),'FaceColor',[1 1 0.5],'EdgeColor',[1 1 0.5]);
        hold on;

        %~~~~~~~~~~~~~~~~~
        % Plot predictor time courses
        %~~~~~~~~~~~~~~~~~

        if ~isempty(plotons)
            bar(sb.onset(plotons,:));
            plot(sb.hemodynamics(:,plotons));
            axis tight;
        end

    end
end
